package com.yunti.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yunti.business.domain.BaseSchool;
import com.yunti.business.service.IBaseSchoolService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学校Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/business/school")
public class BaseSchoolController extends BaseController
{
    @Autowired
    private IBaseSchoolService baseSchoolService;

    /**
     * 查询学校列表
     */
    @PreAuthorize("@ss.hasPermi('business:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseSchool baseSchool)
    {
        startPage();
        List<BaseSchool> list = baseSchoolService.selectBaseSchoolList(baseSchool);
        return getDataTable(list);
    }

    /**
     * 导出学校列表
     */
    @PreAuthorize("@ss.hasPermi('business:school:export')")
    @Log(title = "学校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseSchool baseSchool)
    {
        List<BaseSchool> list = baseSchoolService.selectBaseSchoolList(baseSchool);
        ExcelUtil<BaseSchool> util = new ExcelUtil<BaseSchool>(BaseSchool.class);
        util.exportExcel(response, list, "学校数据");
    }

    @Log(title = "学校管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:school:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseSchool> util = new ExcelUtil<BaseSchool>(BaseSchool.class);
        List<BaseSchool> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = baseSchoolService.importSchool(userList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取学校详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return success(baseSchoolService.selectBaseSchoolById(id));
    }

    /**
     * 新增学校
     */
    @PreAuthorize("@ss.hasPermi('business:school:add')")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseSchool baseSchool)
    {
        return toAjax(baseSchoolService.insertBaseSchool(baseSchool));
    }

    /**
     * 修改学校
     */
    @PreAuthorize("@ss.hasPermi('business:school:edit')")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseSchool baseSchool)
    {
        String operName = getUsername();
        return toAjax(baseSchoolService.updateBaseSchool(baseSchool,operName));
    }

    /**
     * 删除学校
     */
    @PreAuthorize("@ss.hasPermi('business:school:remove')")
    @Log(title = "学校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseSchoolService.deleteBaseSchoolByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BaseSchool> util = new ExcelUtil<BaseSchool>(BaseSchool.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
